<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选状态 -->
			<cl-filter label="状态">
				<cl-select prop="status" :options="options.status" />
			</cl-filter>
			<!-- 筛选权限 -->
			<cl-filter label="权限">
				<cl-select prop="permission" :options="options.permission" />
			</cl-filter>
			<!-- 筛选分类 -->
			<cl-filter label="分类">
				<cl-select prop="category" :options="options.category" />
			</cl-filter>
			<!-- 筛选分类 -->
			<cl-filter label="质量">
				<cl-select prop="qualityType" :options="options.qualityTypeList" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索主播" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table"> </cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>
		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="anchor-info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";
import { useDict } from "/$/dict";

const { dict } = useDict();

const { service } = useCool();

// 选项
const options = reactive({
	labelsList: [
		{ label: "相册", value: 0, type: "danger" },
		{ label: "视频", value: 1, type: "success" }
	],
	status: [
		{ label: "禁用", value: 0, type: "danger" },
		{ label: "启用", value: 1, type: "success" }
	],
	permission: [
		{ label: "普通", value: 0, type: "danger" },
		{ label: "vip", value: 1, type: "success" }
	],
	qualityTypeList: [
		{ label: "普通", value: 1, type: "danger" },
		{ label: "标准", value: 2, type: "success" },
		{ label: "精致", value: 3, type: "primary" }
	],
	category: dict.get("photoType")
});

let anchorOptions = ref<any>([]);

// cl-upsert
const Upsert = useUpsert({
	dialog: {
		width: "40%"
	},
	items: [
		{
			label: "主播",
			prop: "anchor",
			component: {
				name: "el-select",
				options: anchorOptions,
				props: { clearable: true }
			},
			col: {
				span: 12
			},
			required: true
		},
		{
			label: "封面",
			prop: "cover",
			component: { name: "cl-upload" },
			required: true
		},
		{
			label: "图片",
			prop: "image",
			component: {
				name: "cl-upload",
				props: {
					multiple: true,
					limit: 30
				}
			},
			required: true
		},
		{
			label: "时间",
			prop: "releaseTime",
			component: {
				name: "el-date-picker",
				props: {
					clearable: true,
					type: "datetime",
					format: "YYYY-MM-DD HH:mm:ss"
				}
			},
			col: {
				span: 12
			},
			value: "2024-10-12 00:00:00"
		},
		{
			label: "标签",
			prop: "labels",
			component: {
				name: "el-radio-group",
				options: options.labelsList
			},
			col: {
				span: 12
			},
			value: 0
		},
		{
			label: "质量类型",
			prop: "qualityType",
			component: {
				name: "el-radio-group",
				options: options.qualityTypeList
			},
			col: {
				span: 12
			},
			value: 1
		},
		{
			label: "状态",
			prop: "status",
			component: { name: "el-radio-group", options: options.status },
			value: 0,
			col: {
				span: 12
			},
			required: true
		},
		{
			label: "权限",
			prop: "permission",
			component: { name: "el-radio-group", options: options.permission },
			value: 0,
			col: {
				span: 12
			},
			required: true
		},
		{
			label: "分类",
			prop: "category",
			component: { name: "el-radio-group", options: options.category },
			value: 0,
			col: {
				span: 12
			},
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "主播", prop: "anchorName", minWidth: 140 },
		{
			label: "封面",
			prop: "cover",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{
			label: "图片",
			prop: "image",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "标签", prop: "labels", dict: options.labelsList, minWidth: 120 },
		{ label: "质量类型", prop: "qualityType", dict: options.qualityTypeList, minWidth: 120 },
		{ label: "状态", prop: "status", dict: options.status, minWidth: 120 },
		{ label: "权限", prop: "permission", dict: options.permission, minWidth: 120 },
		{ label: "分类", prop: "category", dict: options.category, minWidth: 120 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

let category = ref<any>([]);
// cl-crud
const Crud = useCrud(
	{
		service: service.anchor.info
	},

	async (app) => {
		app.refresh();
	}
);

const GetAnchorOptions = async () => {
	let data = await service.demo.info.list();
	anchorOptions.value = data.map((o: any) => {
		return {
			value: o.id + "",
			label: o.name
		};
	});
};

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

onMounted(() => {
	GetAnchorOptions();
});
</script>
